/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hiberengine;


import hibernate.Persone;
import hibertest.HibernateUtil;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.Session;

/**
 *
 * @author Alexx
 */
public class PersoneDAOImpl implements PersoneDAO {
    
    public void addBus(hibernate.Persone persone) throws SQLException {
    Session session = null;
    try {
      session = HibernateUtil.getSessionFactory().openSession();
      session.beginTransaction();
      session.save(persone);
      session.getTransaction().commit();
    } catch (Exception e) {
      
    } finally {
      if (session != null && session.isOpen()) {

        session.close();
      }
    }
  }

    @Override
    public void updateBus(String id, hibernate.Persone persone) throws SQLException {
        Session session = null;
    try {
      session = HibernateUtil.getSessionFactory().openSession();
      session.beginTransaction();
      session.update(persone);
      session.getTransaction().commit();
    } catch (Exception e) {

    } finally {
      if (session != null && session.isOpen()) {
        session.close();
      }
    }
    }

    @Override
    public hibernate.Persone getBusById(String id) throws SQLException {
        Session session = null;
    hibernate.Persone persone = null;
    try {
      session = HibernateUtil.getSessionFactory().openSession();
      persone = (hibernate.Persone) session.load(hibernate.Persone.class, id);
    } catch (Exception e) {
      
    } finally {
      if (session != null && session.isOpen()) {
        session.close();
      }
    }
    return persone;
    }

    @Override
    public Collection getAllBusses() throws SQLException {
         Session session = null;
    List persons = new ArrayList<hibernate.Persone>();
    try {
      session = HibernateUtil.getSessionFactory().openSession();
      persons = session.createCriteria(hibernate.Persone.class).list();
    } catch (Exception e) {
      
    } finally {
      if (session != null && session.isOpen()) {
        session.close();
      }
    }
    return persons;
    }

    @Override
    public void deleteBus(hibernate.Persone persone) throws SQLException {
        Session session = null;
    try {
      session = HibernateUtil.getSessionFactory().openSession();
      session.beginTransaction();
      session.delete(persone);
      session.getTransaction().commit();
    } catch (Exception e) {

    } finally {
      if (session != null && session.isOpen()) {
        session.close();
      }
    }
    }
}
